package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.Attachment;
import com.ala4.oxcafe.repository.mapper.AttachmentMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 系统附件存储实体
 *
 * @author PING
 * @date 2025-03-25
 */
@Repository
public class AttachmentRepository extends ServiceImpl<AttachmentMapper, Attachment> implements IService<Attachment> {

    private LambdaQueryWrapper<Attachment> queryParam(Attachment attachment) {

        LambdaQueryWrapper<Attachment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(StrUtil.isNotEmpty(attachment.getSearchValue()), item -> {
            item.like(Attachment::getFileName, attachment.getSearchValue())
                    .or().like(Attachment::getFileKey, attachment.getSearchValue());
        });
        queryWrapper.eq(ObjectUtil.isNotNull(attachment.getStorageType()), Attachment::getStorageType, attachment.getStorageType());
        queryWrapper.eq(ObjectUtil.isNotNull(attachment.getRelatedId()), Attachment::getRelatedId, attachment.getRelatedId());

        return queryWrapper;
    }

    public IPage<Attachment> selectAttachmentPage(PageDomain pageDomain, Attachment attachment) {

        return this.page(pageDomain.toPlusPage(), queryParam(attachment));
    }

    public List<Attachment> selectAttachmentList(Attachment attachment) {
        return this.list(queryParam(attachment));
    }
}
